<?php

namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\IntergralLog as IntergralLogModel;
use app\admin\validate\CommonnValidate;
use app\BaseController;

class PcUser extends BaseController
{
    protected $middleware = ['Auth'];
    public function list() {
        $params = $this->request->post(['current', 'size', 'nickname', 'email', 'id']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new UserModel();
        $whereArr = [];
        if(isset($params['id']) && $params['id']) {
            $whereArr[] = ["id", "=", $params['id']];
        }
        if(isset($params['nickname']) && $params['nickname']) {
            $whereArr[] = ["nickname", "LIKE", '%'.$params['nickname'].'%'];
        }
        if(isset($params['email']) && $params['email']) {
            $whereArr[] = ["email", "=", $params['email']];
        }
        $list = $sql
            ->where($whereArr)
            ->order('integral desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ]);
        return result()->success($list);
    }
    public function childer()
    {
        $params = $this->request->post(['user_id', 'nickname', 'email']);
        $sql = new UserModel();
        if(isset($params['nickname']) && $params['nickname']) {
            $whereArr[] = ["nickname", "LIKE", '%'.$params['nickname'].'%'];
        }
        if(isset($params['email']) && $params['email']) {
            $whereArr[] = ["email", "=", $params['email']];
        }
        $whereArr[] = ["superiorUserId", "=", $params['user_id']];
        $list = $sql->where($whereArr)->select();
        return result()->success($list);
    }
    public function detail()
    {
        $params = $this->request->post(['user_id']);
        $sql = new UserModel();
        $info = $sql->withCount(['buy', 'report'])->find($params['user_id']);
        $count = $sql->where('superiorUserId', '=', $params['user_id'])->count();
        $info['childer_count'] = $count;
        return result()->success($info);
    }
    public function integrallog()
    {
        $params = $this->request->post(['current', 'size', 'userId', 'type', 'about_id']);
        $whereArr = [];
        if(isset($params['userId']) && $params['userId']) {
            $whereArr[] = ["userId", "=", $params['userId']];
        }
        if(isset($params['type']) && $params['type']) {
            $whereArr[] = ["type", "=", $params['type']];
        }
        if(isset($params['about_id']) && $params['about_id']) {
            $whereArr[] = ["about_id", "=", $params['about_id']];
        }
        $sql = new IntergralLogModel();
        $list = $sql
            ->where($whereArr)
            ->with('userInfo')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ]);
        return result()->success($list);
    }
}